package com.blensmile.activity;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.os.Bundle;

import com.blensmile.BaseActivity;
import com.blensmile.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LineChartActivity extends BaseActivity {

    @BindView(R.id.lc_chart)
    LineChart lcChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        ButterKnife.bind(this);
        initChart();
    }

    private void initChart() {
        XAxis x = lcChart.getXAxis();
        x.enableGridDashedLine(10f,10f,0f);
//        x.setLabelCount(23);
        x.setLabelCount(12);
        x.setAxisMinimum(1);
//        x.setAvoidFirstLastClipping(true);
        x.setAxisMaximum(24);
        x.setTextColor(0xffa0a0a0);
        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setAxisLineColor(0xffa0a0a0);
        x.setTextSize(11f);
        YAxis y = lcChart.getAxisLeft();
        y.removeAllLimitLines();
        y.enableGridDashedLine(10f,10f,0f);
//        y.setDrawZeroLine(true);
        y.setAxisMaximum(100);
        y.setAxisMinimum(0);
        y.setLabelCount(11);
        y.setTextColor(0xffa0a0a0);
        y.setAxisLineColor(0xffa0a0a0);
        y.setTextSize(11f);
        lcChart.getAxisRight().setEnabled(false);
        setData(24,100);
//        lcChart.getLegend().setForm(Legend.LegendForm.LINE);
        lcChart.setTouchEnabled(false);
        lcChart.getLegend().setEnabled(false);
        lcChart.animateX(2000);
        lcChart.getDescription().setEnabled(false);
    }

    private void setData(int count,float range){
        ArrayList<Entry> values = new ArrayList<Entry>();
        count++;
        for (int i = 1; i < count; i++) {
            float val = (float) (Math.random() * range);
            values.add(new Entry(i, val, getResources().getDrawable(R.drawable.homework)));
        }

//        values.add(new Entry(-1,100));
        LineDataSet set1;

        if (lcChart.getData() != null &&
                lcChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet)lcChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            lcChart.getData().notifyDataChanged();
            lcChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(values, "DataSet 1");

            set1.setDrawIcons(false);

            // set the line to be drawn like this "- - - - - -"
//            set1.enableDashedLine(10f, 5f, 0f);
//            set1.enableDashedHighlightLine(10f, 5f, 0f);
            set1.setColor(0xff58a5fa);
            set1.setCircleColor(0xff58a5fa);
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);
            set1.setDrawCircleHole(false);
            set1.setDrawFilled(true);
            set1.setFormLineWidth(1f);
//            set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
            set1.setFormSize(15.f);
            set1.setValueTextSize(0);

            set1.setFillColor(0xff58a5fa);

            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1); // add the datasets

//            // create a data object with the datasets
            LineData data = new LineData(dataSets);
//
//            // set data
            lcChart.setData(data);

        }
    }
}
